package com.baoming.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.ISingupDao;
import com.baoming.domain.Singup;
import com.baoming.service.ISingupService;
import com.baoming.util.PageInfo;

@Transactional(readOnly=true)
@Service(ISingupService.SERVICE)
public class SingupServiceImpl implements ISingupService {

	@Resource(name=ISingupDao.SERVICE)
	private ISingupDao singupDao ;

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveSingup(Singup singup) {
		singupDao.save(singup) ; 
	}

	/**
	 * 分页查询报名
	 */
	public List<Singup> selectSingupWithPage(PageInfo pageInfo , String memberId) {
		String hql = " and m.isAllow = 1 and m.isDelete = 1 and m.memberId = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<Singup> singups = singupDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return singups ;
	}

	public Singup selectSingUpById(String singId) {
		return singupDao.findObjectById(singId);
	}

	/**
	 * 按时间 分页查询
	 */
	public List<Singup> selectSingupWithPage(String memberId) {
		String hql = " and m.isAllow = 1 and m.isDelete = 1 and m.memberId = ? and m.endTime > now() " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<Singup> singups = singupDao.findCollectionByConditionNoPage(hql, params.toArray(), orderby ) ; 
		return singups ;
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void modifySingup(Singup singup) {
		singupDao.update(singup) ; 
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateQrcode(String codePath , String id) {
		singupDao.updateQrcode(codePath , id) ; 
	}

	/**
	 * 分布搜索比赛信息
	 */
	public List<Singup> selectSingupWithPage(String keyword, PageInfo pageInfo,
			String memberId) { 
		String hql = " and m.isAllow = 1 and m.isDelete = 1 and  m.type = 'b' and m.memberId = ? and m.title like ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		params.add(keyword+"%") ; 
		LinkedHashMap<String , String> orderby = new LinkedHashMap<String , String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<Singup> singups = singupDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return singups ;
	} 
	
}
